using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Customer;
using System.Net;
using System.Net.Sockets;


namespace WindowsFormsApplication1
{
    public partial class MixedDrinkForm : Form
    {        
        string cardNum;
        Person person;
        BitArray status;
        string[] drink = new string[14];
        string cost;
        bool ServerDetected = false;
        string userName;
        string password;
        string ipAddress = "10.40.136.93";//"10.40.163.142";//"10.40.163.142";//"10.40.167.6";//"10.40.137.203";//"10.254.254.253";
        int portNumber = 9050;

        // variables to send data to bartender
        string stringData;
        byte[] data;
        IPEndPoint ipep;
        Socket server;
        int count;
        string amount;
        string drinkOrderRevised;
        string drinkName;

        // variables to receive data from bartender
        IPEndPoint sender;
        EndPoint Remote;
        int recv;
        
        public MixedDrinkForm(Person Person, BitArray Status)
        {
            person = Person;
            status = Status;
            drink = Person.Drink;
            cardNum = person.CardNumber;
            userName = person.Username;
            password = person.Password;            
            InitializeComponent();            
        }        

        public void drinkOrdered(string Drink, string Cost)
        {
            stringData = SimpleUdpClient(Drink);
            if (ServerDetected) {
                person.SessionTotal = person.SessionTotal + Convert.ToDecimal(cost);
                ThankYouForm thankYouForm = new ThankYouForm(person, stringData, status);
                this.Close();
                thankYouForm.ShowDialog();
            }
            else {
                System.Windows.Forms.MessageBox.Show("No Server Detected");
                this.Close();
            }
        }

        private void Drink1_Click(object sender, EventArgs e)
        {            
            drinkName = Drink1.Text;
            cost = drink[1];
            drinkOrdered(drinkName, cost);            
        }

        private void Drink2_Click(object sender, EventArgs e)
        {            
            drinkName = Drink2.Text;
            cost = drink[3];
            drinkOrdered(drinkName, cost);
        }

        private void Drink3_Click(object sender, EventArgs e)
        {            
            drinkName = Drink3.Text;
            cost = drink[5];
            drinkOrdered(drinkName, cost);
        }

        private void Drink4_Click(object sender, EventArgs e)
        {
            drinkName = Drink4.Text;
            cost = drink[7];
            drinkOrdered(drinkName, cost);
        }

        private void Drink5_Click(object sender, EventArgs e)
        {
            drinkName = Drink5.Text;
            cost = drink[9];
            drinkOrdered(drinkName, cost);
        }

        private void Drink6_Click(object sender, EventArgs e)
        {            
            drinkName= Drink6.Text;
            cost = drink[11];
            drinkOrdered(drinkName, cost);
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public string SimpleUdpClient(string drinkOrder)
        {            
            data = new byte[2048];                
            ipep = new IPEndPoint(IPAddress.Parse(ipAddress), portNumber);
            server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            count = 1000;

            // convert amount to string to send to server
            amount = count.ToString();
            drinkOrderRevised = "#" + drinkOrder + "#" + person.CardNumber + "#" + cost + "#";
            data = Encoding.ASCII.GetBytes(drinkOrderRevised);
            // try sending the order to the bartender
            try {
                server.SendTo(data, data.Length, SocketFlags.None, ipep);
            }
            catch (SocketException ex) {
                string message = ex.Message;
                System.Windows.Forms.MessageBox.Show(message);
            }

            sender = new IPEndPoint(IPAddress.Any, 0);
            Remote = (EndPoint)sender;
            data = new byte[2048];

            // try to receive the data send from the server
            try {
                recv = server.ReceiveFrom(data, ref Remote);
                stringData = Encoding.ASCII.GetString(data, 0, recv);
                ServerDetected = true;
                return stringData;
            }
            catch (SocketException ex) {
                string message = ex.Message;
                System.Windows.Forms.MessageBox.Show(message + " \nYou will not be charged for this order");
                stringData = "No Host Detected";
                ServerDetected = false;
                return stringData;
            } 
        }

        private void MixedDrinkForm_Load(object sender, EventArgs e)
        {
            
            Drink1.Text = drink[0];
            price1.Text = "$" + drink[1];
            Drink2.Text = drink[2];
            price2.Text = "$" + drink[3];
            Drink3.Text = drink[4];
            price3.Text = "$" + drink[5];
            Drink4.Text = drink[6];
            price4.Text = "$" + drink[7];
            Drink5.Text = drink[8];
            price5.Text = "$" + drink[9];
            Drink6.Text = drink[10];
            price6.Text = "$" + drink[11];     

        }

        private void helpButton_Click(object sender, EventArgs e)
        {
            string helpMessage = "Please choose one of the mixed drinks from the order form. " + 
                "Your order will be sent to the bartender and your card will be charged with " +
                "the price shown underneath the drink";
            HelpForm helpForm = new HelpForm(helpMessage);
            helpForm.ShowDialog();
        }
    }
}


//string path1 = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
//string[] path2 = { path1 + "\\newFile.txt"};
//string path="C:\\Users\\eclark4\\Desktop\\WriteLines1.txt";